package com.logictech.lims.web.back;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jeecgframework.p3.core.common.utils.AjaxJson;
import org.jeecgframework.p3.core.util.SystemTools;
import org.jeecgframework.p3.core.util.plugin.ContextHolderUtils;
import org.jeecgframework.p3.core.util.plugin.ViewVelocity;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.jeecgframework.p3.core.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.logictech.activity.system.vo.LoginUser;
import com.logictech.lims.entity.GlasswareType;
import com.logictech.lims.service.GlasswareTypeService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 描述：</b>GlasswareTypeController<br>
 * 
 * @author guoqi
 * @since：2017年03月23日 13时02分41秒 星期四
 * @version:1.0
 */
@Controller
@RequestMapping("/lims/back/glasswareType")
public class GlasswareTypeController extends BaseController {
	@Autowired
	private GlasswareTypeService glasswareTypeService;

	/**
	 * 列表页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "list", method = { RequestMethod.GET, RequestMethod.POST })
	public void list(@ModelAttribute GlasswareType query, HttpServletResponse response, HttpServletRequest request,
			@RequestParam(required = false, value = "pageNo", defaultValue = "1") int pageNo,
			@RequestParam(required = false, value = "pageSize", defaultValue = "10") int pageSize) throws Exception {
		PageQuery<GlasswareType> pageQuery = new PageQuery<GlasswareType>();
		pageQuery.setPageNo(pageNo);
		pageQuery.setPageSize(pageSize);
		VelocityContext velocityContext = new VelocityContext();
		pageQuery.setQuery(query);
		velocityContext.put("glasswareType", query);
		velocityContext.put("pageInfos",
				SystemTools.convertPaginatedList(glasswareTypeService.queryPageList(pageQuery)));
		String viewName = "lims/back/glasswareType-list.vm";
		request.getSession().setAttribute("pageQueryGlasswareType", pageQuery);
		ViewVelocity.view(request, response, viewName, velocityContext);
	}
	
	/**
	 * 返回列表页面
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "back", method = { RequestMethod.GET, RequestMethod.POST })
	public void back(@ModelAttribute GlasswareType query, HttpServletResponse response, HttpServletRequest request,
			@RequestParam(required = false, value = "pageNo", defaultValue = "1") int pageNo,
			@RequestParam(required = false, value = "pageSize", defaultValue = "10") int pageSize) throws Exception {
		PageQuery<GlasswareType> pageQuery = new PageQuery<GlasswareType>();
		
		Object session = request.getSession().getAttribute("pageQueryGlasswareType");
		if(session != null){
			pageQuery = (PageQuery<GlasswareType>)session;
			query = pageQuery.getQuery();
		}else{
			pageQuery.setPageNo(pageNo);
			pageQuery.setPageSize(pageSize);
			pageQuery.setQuery(query);
		}
		
		VelocityContext velocityContext = new VelocityContext();
		velocityContext.put("glasswareType", query);
		velocityContext.put("pageInfos",
				SystemTools.convertPaginatedList(glasswareTypeService.queryPageList(pageQuery)));
		String viewName = "lims/back/glasswareType-list.vm";
		ViewVelocity.view(request, response, viewName, velocityContext);
	}

	/**
	 * 详情
	 * 
	 * @return
	 */
	@RequestMapping(value = "toDetail", method = RequestMethod.GET)
	public void toDetail(@RequestParam(required = true, value = "id") String id,
			HttpServletResponse response, HttpServletRequest request) throws Exception {
		VelocityContext velocityContext = new VelocityContext();
		String viewName = "lims/back/glasswareType-detail.vm";
		GlasswareType glasswareType = glasswareTypeService.queryById(id);
		velocityContext.put("glasswareType", glasswareType);
		ViewVelocity.view(request, response, viewName, velocityContext);
	}

	/**
	 * 跳转到添加页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/toAdd", method = { RequestMethod.GET, RequestMethod.POST })
	public void toAdd(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {
		VelocityContext velocityContext = new VelocityContext();
		String viewName = "lims/back/glasswareType-add.vm";
		ViewVelocity.view(request, response, viewName, velocityContext);
	}

	/**
	 * 保存信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/doAdd", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public AjaxJson doAdd(@ModelAttribute GlasswareType glasswareType) {
		AjaxJson j = new AjaxJson();
		try {
			LoginUser user = (LoginUser) ContextHolderUtils.getSession().getAttribute("OPERATE_WEB_LOGIN_USER");
			glasswareType.setCreateUser(user.getId().toString());
			glasswareType.setCreateTime(new Date());
			glasswareType.setUpdateUser(user.getId().toString());
			glasswareType.setUpdateTime(new Date());
			glasswareType.setDeleteFlag("0");
			glasswareTypeService.doAdd(glasswareType);
			j.setMsg("保存成功");
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("保存失败");
		}
		return j;
	}

	/**
	 * 跳转到编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "toEdit", method = RequestMethod.GET)
	public void toEdit(@RequestParam(required = true, value = "id") String id, HttpServletResponse response,
			HttpServletRequest request) throws Exception {
		VelocityContext velocityContext = new VelocityContext();
		GlasswareType glasswareType = glasswareTypeService.queryById(id);
		velocityContext.put("glasswareType", glasswareType);
		String viewName = "lims/back/glasswareType-edit.vm";
		ViewVelocity.view(request, response, viewName, velocityContext);
	}

	/**
	 * 编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "/doEdit", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public AjaxJson doEdit(@ModelAttribute GlasswareType glasswareType) {
		AjaxJson j = new AjaxJson();
		try {
			LoginUser user = (LoginUser) ContextHolderUtils.getSession().getAttribute("OPERATE_WEB_LOGIN_USER");
			glasswareType.setUpdateUser(user.getId().toString());
			glasswareType.setUpdateTime(new Date());
			glasswareTypeService.doEdit(glasswareType);
			j.setMsg("编辑成功");
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("编辑失败");
		}
		return j;
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "doDelete", method = RequestMethod.GET)
	@ResponseBody
	public AjaxJson doDelete(@RequestParam(required = true, value = "id") String id) {
		AjaxJson j = new AjaxJson();
		try {
			glasswareTypeService.doDelete(id);
			j.setMsg("删除成功");
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("删除失败");
		}
		return j;
	}

	/**
	 * 获取规格列表
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = { "/getSizeList" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET,
			org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public AjaxJson getSizeList(HttpServletRequest request,@RequestParam(required = true, value = "type" ) String type) {
		AjaxJson j = new AjaxJson();
		try {
			List<GlasswareType> sizeList = this.glasswareTypeService.querySizeList(type);
			JSONArray result = new JSONArray();
			if ((sizeList != null) && (sizeList.size() > 0)) {
				for (int i = 0; i < sizeList.size(); i++) {
					JSONObject jsonParts = new JSONObject();
					jsonParts.put("id", sizeList.get(i).getId());
					jsonParts.put("size", sizeList.get(i).getSize());
					result.add(jsonParts);
				}
			}
			Map<String, Object> attrs = new HashMap<String, Object>();
			attrs.put("sizeList", result);
			j.setAttributes(attrs);
			j.setSuccess(true);
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("访问异常！");
			this.LOG.error("getSizeList error:{}", e.getMessage());
		}
		return j;
	}

}
